<!DOCTYPE html>
<html class="ui-page-login">
	<head>
		<title>奖品发放平台</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/v1.6.css" rel="stylesheet"  />
		<link href="css/waitMe.min.css" rel="stylesheet" />
	</head>
	<body class="login">
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">奖品发放平台</h1>
		</header>
		<div class="mui-content">
			<form id='login-form' class="mui-input-group">
				<div class="mui-input-row">
					<label>活动名称</label>
					<input id="activityName" type="text" readonly value=""></input>
				</div>
				<div class="mui-input-row">
					<label>登录口令</label>
					<input id='password' type="password" class="mui-input-clear mui-input" value="" placeholder="请输入商家口令">
				</div>
			</form>
			<div class="mui-content-padded">
				<button id='login' class="mui-btn mui-btn-block mui-btn-primary">登录</button>
				<div class="link-area"></div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script>
            
		</script>
	</body>
	<script src="js/mui.min.js"></script>
	<script src="js/jquery.min.js"></script>
	<script src="js/handlebars.min.js"></script>
	<script src="js/waitMe.min.js"></script>
	<script src="js/common.js"></script>
	<script src="js/base.js"></script>
	<script src="js/menuFlat/menuFlat.min.js"></script>
	<script src="js/setting.js"></script>
	
	<script type="text/javascript">	  
	var h = mui(document).common();
	
	h.startMask();
	var activityId = h.getQueryString("id");

	mui.ajax(setting.serverUrl + "/interfaces/prize/info",{
		dataType: 'json', //服务器返回json格式数据 
		type: 'get', //HTTP请求类型 
		data:{id: activityId},
		success: function(response) {
			h.endMask();

			if(response.result){
				document.querySelector("#activityName").value = response.data.name;
			}
			else{
				mui.toast(response.message);
			}
		},
		error: function() {
			mui.toast('您的网络出现异常,请稍后再试');
			h.endMask();
		}
	});
	
	//登录	
	 mui(".mui-content").on('tap', '.mui-content-padded', function(e) {		 
		var password = document.getElementById("password").value;

		if(password.length!=0){
		 	h.startMask();

			mui.ajax(setting.serverUrl + "/interfaces/prize/login", {
				dataType: 'json', //服务器返回json格式数据 
				type: 'post', //HTTP请求类型 
				data:{
					id : activityId,
					password: password
				},
				success: function(response) {
					h.endMask();

					if(response.result){
						//退出时用
						sessionStorage.setItem("activityId", activityId);
						sessionStorage.setItem("token", response.data);
						
						var code = h.getQueryString("code");

						if(code!=null && code.length>0){
							confirmCode(code);
						}
						else{
							var htype = {
								url: "home.html"
							}

							h.forward_wechat(htype);
						}
					}else{
						alert(response.message);
					}
				},
				error: function() {
					mui.toast('您的网络出现异常,请稍后再试');
					h.endMask();
				}
			}); 
		}else{
			mui.toast("口令不能为空");
		}
	 });

	 function confirmCode(code){
		h.startMask();

		mui.ajax(setting.serverUrl + "/interfaces/prize/confirm", {
			dataType: 'json', //服务器返回json格式数据 
			type: 'post', //HTTP请求类型 
			data: { 
				code: code,
				token : sessionStorage.getItem("token") 
			},
			success: function (response) {
				h.endMask();

				if (response.result) {
					var htype = {
						url: 'exchange-confirm.html?recordId=' + response.data + "&code=" + code
					}

					h.forward_wechat(htype);
				} else {
					mui.toast("<h3>" + response.message + "</h3>");
				}
			},
			error: function () {
				h.endMask();

				mui.toast('您的网络出现异常,请稍后再试');
			}
		});
	 }
	</script>
</html>